package cn.dgut.training.service.impl;

import cn.dgut.training.dto.AddressDTO;
import cn.dgut.training.entity.Address;
import cn.dgut.training.entity.Region;
import cn.dgut.training.mapper.AddressMapper;
import cn.dgut.training.mapper.RegionMapper;
import cn.dgut.training.service.IAddressService;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.addressVO.AddressVO;
import cn.dgut.training.vo.pageVO.PageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Resource
    private  AddressMapper addressMapper;


    /*
     *根据 用户userId 或 收货人名称name 或 订单id 查询
     * 注意:根据单字段查询时，勿返回多个属性字段
     */
    public List<Address> SelectAddress(AddressDTO addressDTO){

        //获取条件查询构造器
        QueryWrapper queryWrapper = AddressDTO.getWrapper(addressDTO);

        List<Address> addressList = addressMapper.selectList(queryWrapper);


        return addressList;
    }


    /*
    *分页显示
     */
    public PageVO listByPage(List<Address> list){

        PageVO pageVO=new PageVO("address",list);

        return pageVO;

    }



    /*
     *展示默认地址
     */
    public Address getDefault(AddressDTO addressDTO){

        //获取条件查询构造器
        QueryWrapper queryWrapper = AddressDTO.getDefault(addressDTO);

        Address address = addressMapper.selectOne(queryWrapper);

        return address;


    }

    /**
     * 获取默认地址
     * @param isDefault
     * @param userId
     * @return
     */
    public List<Address> UniqueDefault(Boolean isDefault,Integer userId) {
        if (isDefault) {
            QueryWrapper queryWrapper = new QueryWrapper();
            Map<String, Integer> map = new HashMap<>();
            map.put("is_default", 1);
            map.put("user_id", userId);
            queryWrapper.allEq(map);
            List<Address> addresses = addressMapper.selectList(queryWrapper);
            for (Address address : addresses) {
                address.setIsDefault(false);
            }
            return addresses;
        }
        return null;
    }
}
